<?php

class articlesActions extends sfActions {

    public function executeIndex(sfWebRequest $request){
        $c = new Criteria();
        $c->add(ArticlesPeer::ENABLED, 1);
        $c->addAscendingOrderByColumn(PublicationsPeer::PUBLICATIONS_TYPE_ID);
        $c->addDescendingOrderByColumn(PublicationsPeer::TITLE);
        $c->addAscendingOrderByColumn(ArticlesPeer::NUMBER);

        $c->addJoin(ArticlesPeer::PUBLICATIONS_ID, PublicationsPeer::ID);

        $pager = new sfPropelPager('Articles', sfConfig::get('app_max_per_page'));
        $pager->setCriteria($c);
        $pager->setPage($request->getParameter('page', 1));
        $pager->init();
        $this->pager = $pager;
    }

    public function executeNew(sfWebRequest $request)
    {




    }

    public function executeNew_save(sfWebRequest $request){
        $title = $request->getParameter('title');
        $author = $request->getParameter('author');
        $number = $request->getParameter('number');
        $publication_id = $request->getParameter('publication_id');
        $pdf = $_FILES["pdf"]["tmp_name"];

        if($publication_id == ""){
            $this->getUser()->setFlash('msg','El artículo debe estar enlazado a una publicación');
            $this->redirect('@articles_new');
        }

        $article = new Articles();
        $article->setTitle($title);
        $article->setAuthor($author);
        $article->setPublicationsId($publication_id);
        $article->setEnabled(1);
        $article->setNumber($number);
        $article->setCreatedAt(date("Y-m-d H:i:s"));
        $article->save();
        
        $article->setFilename($article->getId()."_".$_FILES["pdf"]["name"]);
        $article->save();

        if($pdf!=''){
            $DS = DIRECTORY_SEPARATOR;
            move_uploaded_file($pdf, sfConfig::get('sf_upload_dir').$DS."article".$DS.$article->getId().'.pdf');
        }

        $this->getUser()->setFlash('msg','Artículo creado satisfactoriamente');
        $this->redirect('@articles');
    }

    public function executeEdit(sfWebRequest $request){
        $id = $request->getParameter('id');
        $this->article = ArticlesPeer::retrieveByPK($id);
    }

    public function executeEdit_save(sfWebRequest $request){
        $hdId = $request->getParameter('hdId');
        $title = $request->getParameter('title');
        $author = $request->getParameter('author');
        $number = $request->getParameter('number');
        $publication_id = $request->getParameter('publication_id');
        $pdf = $_FILES["pdf"]["tmp_name"];

        if($publication_id == ""){
            $this->getUser()->setFlash('msg','El artículo debe estar enlazado a una publicación');
            $this->redirect('@articles_edit');
        }

        $article = ArticlesPeer::retrieveByPK($hdId);
        $article->setTitle($title);
        $article->setAuthor($author);
        $article->setPublicationsId($publication_id);
        $article->setFilename($article->getId()."_".$_FILES["pdf"]["name"]);
        $article->setNumber($number);
        $article->save();

        if($pdf!=''){
            $DS = DIRECTORY_SEPARATOR;
            move_uploaded_file($pdf, sfConfig::get('sf_upload_dir').$DS."article".$DS.$article->getId().'.pdf');
        }

        $this->getUser()->setFlash('msg','Artículo modificado satisfactoriamente');
        $this->redirect('@articles');
    }

    public function executeDelete(sfWebRequest $request){
        $id = $request->getParameter('id');
        $article = ArticlesPeer::retrieveByPK($id);
        $article->setEnabled(0);
        $article->save();

        $this->getUser()->setFlash('msg','Artículo eliminado satisfactoriamente');
        $this->redirect('@articles');
    }

    public function executeAjax_search_publications_by_title(sfWebRequest $request){
        $title = $request->getParameter('title', '');

        $c = new Criteria();
        if($title!=""){
            $c->add(PublicationsPeer::TITLE, "%".$title."%", Criteria::LIKE);
        }
        $c->add(PublicationsPeer::ENABLED, 1);
        $c->addAscendingOrderByColumn(PublicationsPeer::TITLE);
        $c->setLimit(20);
        $this->publications = PublicationsPeer::doSelect($c);
    }

}